import { defineStore } from "pinia";
import { ref } from "vue";
import { getCurrentUser } from "@/api/userController";
import ACCESS_CONSTANT from "@/access/accessConstant";

export const useLoginUserStore = defineStore("loginUser", () => {
  const loginUser = ref<API.UserVO>({
    userName: "未登录",
  });

  async function fetchLoginUser() {
    const res = await getCurrentUser();
    console.log("我获取当前登录用户了", res);
    if (res.data.code === 20000 && res.data.data) {
      loginUser.value = res.data.data;
      console.log("我设置值了", loginUser);
      console.log("我设置值了loginUser.value", loginUser.value);
    } else {
      loginUser.value = { userRole: ACCESS_CONSTANT.NOT_LOGIN };
    }
  }

  function setLoginUser(newLoginUser: API.UserVO) {
    loginUser.value = newLoginUser;
  }

  return { loginUser, setLoginUser, fetchLoginUser };
});
